<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import {ref} from 'vue';
import {useRouter} from 'vue-router';
import {showFailToast, showSuccessToast,showLoadingToast,closeToast  } from 'vant';
import {encryptAsDoNet} from "@/util/desUtil.js";
import {urlPath} from "@/api/index/urlPath.js";
import {routerPath} from "@/router/routerPath.js";
import ZYBW from "@/util/request/index.js";

// 获取 router 实例
const router = useRouter();
// const username = ref('石家庄市春苗种子经营部');
// const password = ref('1234567Aa!');

// const username = ref('hmjtest');
// const password = ref('Hmjtest@888');
const username = ref('奈曼旗大镇三丰农资经销部');
const password = ref('987456Pplm');
//http://202.127.42.144:60011/api/newarchive//api/seedarchive/sys/appLogin
//http://172.16.103.57:8888/zytapi/api/seedarchive/sys/appLogin
const forbidClick = ref(true);


const onSubmit = (values) => {
  console.log('submit', values);
  var data={
    username:username.value,
    password:encryptAsDoNet( password.value)
  }
  ZYBW.postObj({
    url:urlPath.login,
    data:data,
    start:function start() {
      console.log('start2','aaaaaaaaaaaa')
      showLoadingToast({
        message: '加载中...',
        duration: 0, // 设置为 0 表示持续展示，不自动关闭
        forbidClick: forbidClick.value,// 禁止背景点击
      });
    },
    succeed:function succeed(res) {
      // debugger;
      if (200!=res.code){
        showFailToast(res.msg);
        return
      }
      localStorage.setItem(urlPath.keys.token,res.data.Tokens);
      localStorage.setItem(urlPath.keys.user,JSON.stringify(res.data));
      var un=res.data.UserLoginName
      console.log('uname1======',un)
      localStorage.setItem(urlPath.keys.username,un);
      localStorage.setItem(urlPath.keys.uid,res.data.UserID);
      const token = localStorage.getItem('token')
      const uname= localStorage.getItem(urlPath.keys.username)
      const uid= localStorage.getItem('uid')

      console.log('Authorization-l======',token)
      console.log('uname======',uname,res.data.UserLoginName, localStorage.getItem(urlPath.keys.username))
      console.log('uid======',uid)
      router.push({path: routerPath.main});
    },
    failed:function failed(err) {
      // debugger;
      showFailToast('登录失败：'+err);
    },
    finish:function finish(type) {
      // debugger;
      console.log('finish=', type)
      closeToast();
    }
  })

};


</script>

<style scoped lang="less">

</style>